
import Router from  'koa-router'
import koaBody from 'koa-body'
import path from 'path'
import config from 'config'
import { AppContext, AppState } from 'ROOT/interface/App';
import checkToken from 'ROOT/middleware/checkToken';
import checkAdmin from 'ROOT/middleware/checkAdmin';

import CommonController from 'ROOT/module/common/common.controller';
import * as UserController from 'ROOT/module/user/user.controller';
import * as TradeAccountController from 'ROOT/module/trade-account/TradeAccount.controller';
import * as ExchangeAccountController from 'ROOT/module/exchange-account/ExchangeAccount.controller';
import * as KLineHistoryController from 'ROOT/module/kline-history/KLineHistory.controller';
import * as WatchController from 'ROOT/module/watch/watch.controller';

import * as OrderController from 'ROOT/module/order/order.controller';
import SymbolOrderConfigController from 'ROOT/module/symbol-order-config/SymbolOrderConfig.controller';
import AutoOrderHistoryController from 'ROOT/module/order-history/AutoOrderHistory.controller';
import StrategyController from 'ROOT/module/strategy/strategy.controller';
import SymbolFactorController from 'ROOT/module/symbol-factor/SymbolFactor.controller';
import ExchangeController from 'ROOT/module/exchange/Exchange.controller';

import { mkdir } from 'ROOT/utils';
// /index
const apiPrefix = '/api';
const publicPath = config.get<string>('publicPath');

mkdir(path.join(publicPath, '/upload')).then(() => mkdir(path.join(publicPath, '/upload/temp')));

const router = new Router<AppState, AppContext>();
export default router;

router.get(`${apiPrefix}/common/debug`, checkToken, CommonController.setDebug);
// router.get(`${apiPrefix}/user/users`, checkToken, UserController.get);
router.get(`${apiPrefix}/user/currentUser`, checkToken, UserController.userInfo);
router.get(`${apiPrefix}/user/firstUser`, UserController.createFirstUser);
router.get(`${apiPrefix}/user/invite-code`, checkToken, UserController.getInviteCode);
router.post(`${apiPrefix}/user/invite-code`, checkToken, checkAdmin, UserController.inviteCode);
router.post(`${apiPrefix}/user/create`, UserController.createUser);
router.post(`${apiPrefix}/login/account`, UserController.login);
router.post(`${apiPrefix}/login/exit`, UserController.exit);

router.get(`${apiPrefix}/trade-account`, checkToken, TradeAccountController.get);
router.post(`${apiPrefix}/trade-account`, checkToken, TradeAccountController.updateOne);
router.delete(`${apiPrefix}/trade-account`, checkToken, TradeAccountController.removeOne);
router.get(`${apiPrefix}/exchange-account/rewrite`, checkToken, checkAdmin, ExchangeAccountController.rewrite);

router.get(`${apiPrefix}/watch-symbol`, WatchController.get);
router.post(`${apiPrefix}/watch-symbol`, checkToken, checkAdmin, WatchController.updateOne);
router.post(`${apiPrefix}/watch-symbol/update-strategy`, checkToken, checkAdmin, WatchController.bindStrategy);
router.delete(`${apiPrefix}/watch-symbol`, checkToken, checkAdmin, WatchController.removeOne);


router.get(`${apiPrefix}/kline-history`, KLineHistoryController.get);

// https://trader.8and1.cn/api/kline/sync?symbol=DOGEUSDT&start=2019-06-01&end=2022-12-31
router.get(`${apiPrefix}/kline/sync`, checkToken, KLineHistoryController.syncKLineData);
router.post(`${apiPrefix}/kline-fillmiss`, checkToken, checkAdmin, KLineHistoryController.fillSymbolMissData);
router.post(`${apiPrefix}/kline-history/update`,checkToken, checkAdmin, KLineHistoryController.batchUpdate);

router.get(`${apiPrefix}/symbol-order-config`, checkToken, SymbolOrderConfigController.index);
router.post(`${apiPrefix}/symbol-order-config`, checkToken, SymbolOrderConfigController.updateOne);
router.delete(`${apiPrefix}/symbol-order-config`, checkToken, SymbolOrderConfigController.removeOne);
router.post(`${apiPrefix}/order/watch`, checkToken, SymbolOrderConfigController.watchOrderRisk);

// router.post(`${apiPrefix}/safe-order`, checkToken, SafeOrderController.order);
// router.post(`${apiPrefix}/stop-order`, checkToken, SafeOrderController.stopOrder);

// router.post(`${apiPrefix}/order/safe`, checkToken, SafeOrderController.order);

router.get(`${apiPrefix}/order/history`, checkToken, AutoOrderHistoryController.index);
router.post(`${apiPrefix}/order/sync`, checkToken, AutoOrderHistoryController.getStatusWithUpdate);
router.get(`${apiPrefix}/order/trigger`, checkToken, checkAdmin, OrderController.triggerOrder);
router.get(`${apiPrefix}/order/beforeOrderConfig`, checkToken, AutoOrderHistoryController.getBeforeOrderConfig);
router.post(`${apiPrefix}/order/cancel`, checkToken, AutoOrderHistoryController.cancelOrder);
router.post(`${apiPrefix}/order/submit`, checkToken, OrderController.submitOrder);

router.get(`${apiPrefix}/strategy/analyser`, checkToken, StrategyController.index);
router.get(`${apiPrefix}/strategy/analyser/tourist`, StrategyController.tourist);


router.get(`${apiPrefix}/strategy/symbol-strategy-list`, checkToken, StrategyController.getUserSymbolStrategyList);
router.post(`${apiPrefix}/strategy/symbol-strategy-list/bind`, checkToken, checkAdmin, StrategyController.updateUserSymbolStrategyByAdmin);
router.get(`${apiPrefix}/strategy/symbol-strategy-list`, checkToken, StrategyController.getAllSymbolStrategyList);
router.get(`${apiPrefix}/strategy/strategy-list`, checkToken, checkAdmin, StrategyController.getAllStrategyList);
router.get(`${apiPrefix}/strategy/detail`, checkToken, StrategyController.strategyFunction);
router.get(`${apiPrefix}/strategy/default`, checkToken, StrategyController.defaultStrategyFunction);
router.post(`${apiPrefix}/strategy/update`, checkToken, checkAdmin, StrategyController.updateStrategyFunction);
router.post(`${apiPrefix}/strategy/add`, checkToken, checkAdmin, StrategyController.updateStrategyFunction);
router.post(`${apiPrefix}/strategy/delete`, checkToken, checkAdmin, StrategyController.deleteStrategyFunction);
router.post(`${apiPrefix}/strategy/train`, StrategyController.train);

router.get(`${apiPrefix}/strategy/turning-points`, checkToken, SymbolFactorController.getTradingPoints);
router.post(`${apiPrefix}/strategy/rewrite-vector`, checkToken, SymbolFactorController.rewriteVector);
router.post(`${apiPrefix}/strategy/statistics-vector`, checkToken, SymbolFactorController.statisticsVector);
router.post(`${apiPrefix}/symbol-factor/to-vector`, checkToken, checkAdmin, SymbolFactorController.priceToVector);
// router.post(`${apiPrefix}/symbol-factor/search-vector`, checkToken, SymbolFactorController.searchVector);


router.get(`${apiPrefix}/exchange/asset`, checkToken, ExchangeController.getAsset);
router.post(`${apiPrefix}/exchange/assetTransfer`, checkToken, ExchangeController.assetTransfer);
router.get(`${apiPrefix}/exchange/order/positions`, checkToken, ExchangeController.getPositions);
router.get(`${apiPrefix}/exchange/order/risk-positions`, checkToken, ExchangeController.getRiskPositions);